//
//  AccountDownloadCell.m
//  ImGuider
//
//  Created by llt on 2017/6/7.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountDownloadCell.h"

@interface AccountDownloadCell ()

@property (weak, nonatomic) IBOutlet UIImageView *downloadImageView;

@property (weak, nonatomic) IBOutlet UILabel *downloadNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *totoalSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *downloadStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *scenicCountLabel;

@end

@implementation AccountDownloadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeState:) name:kDownloadCompleteDownNotif object:nil];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setLineModel:(LineModel *)lineModel {
    
    _lineModel = lineModel;
    
    
    [self.downloadImageView setImageURL:[NSURL URLWithString:lineModel.pictures] placeholderImage:[UIImage imageNamed:@"placeholder_squ"]];
    self.downloadNameLabel.text = lineModel.linename;
    self.timeLabel.text = [NSString stringWithFormat:@"%@min",lineModel.timelabel.length > 0 ? lineModel.timelabel : @"--"];
    
    CGFloat progress = 0;
    DownloadState state = [[DownloadManager sharedManager] downloadSateForModel:self.lineModel progress:&progress];
    
    if (lineModel.state == DownloadStateFailed || state == DownloadStateFailed) {
        
        self.lineModel.state = DownloadStateFailed;
        
        self.totoalSizeLabel.text = @"";
        self.downloadStateLabel.text = MYLocalizedString(@"MYDOWNLOAD_STATE_FAILED", nil);
        
    } else if (lineModel.state == DownloadStateCompleted || state == DownloadStateCompleted) {
        
        self.lineModel.state = DownloadStateCompleted;
        
        self.downloadStateLabel.text = [NSString stringWithFormat:@"(%@)",MYLocalizedString(@"MYDOWNLOAD_STATE_SUCCESS", nil)];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            long long bytes = 0;
            
            for (ScenicPoint *point in lineModel.records) {
                
                bytes += [[DownloadManager sharedManager] localFileSizeForURLString:point.playpath];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^ {
                self.totoalSizeLabel.text = [IGUtils changeFileSize:bytes]; 
            });
        });
    } else if (lineModel.state == DownloadStateSuspened) {
        self.totoalSizeLabel.text = @"";
        self.downloadStateLabel.text = MYLocalizedString(@"MYDOWNLOAD_STATE_SUSPEND", nil);
        
    } else {
        
        self.totoalSizeLabel.text = @"";
        self.downloadStateLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"MYDOWNLOAD_STATE_DOWNLOADING", nil),lineModel.progress *100];
    }
    
    self.scenicCountLabel.text = lineModel.recordcount;
    
    self.scenicCountLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"MYDOWNLOAD_TOTAL", nil),lineModel.recordcount];
}

- (void)changeState:(NSNotification *)notif {
    
    if (self.lineModel.state != DownloadStateDownloading) {
        return;
    }
    
    NSString *lineID = notif.userInfo[kDownloadNotifLineID];
    
    if (![lineID isEqualToString:self.lineModel.unionid]) {
        return;
    }
    
    CGFloat progress;
    DownloadState state = [[DownloadManager sharedManager] downloadSateForModel:self.lineModel progress:&progress];
    
    dispatch_async(dispatch_get_main_queue(), ^ {
        
        self.lineModel.progress = progress;
        self.lineModel.state = state;
        [self setLineModel:_lineModel];
    });
}

@end
